package com.rst.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.rst.dao.StudentDAO;
import com.rst.model.Section;
import com.rst.model.Student;
import com.rst.model.StudentCategory;
import com.rst.model.UserDetailsDTO;
import com.rst.service.SectionService;
import com.rst.service.StudentService;
import com.rst.view.AdmissionFormView;

 
@Controller
public class StudentController {
	
	@Autowired
	private StudentDAO studentDAO;

	@Autowired
	private StudentService studentService;
	
	@Autowired
	private SectionService sectionService;
	

	@InitBinder
	public void initBinder(WebDataBinder binder) 
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}


	@RequestMapping(value="/saveStudent", method=RequestMethod.GET)
	public ModelAndView newStudentForm()
	{
		ModelAndView mav = new ModelAndView("student/newStudent");
		Student student = new Student(); 
		mav.getModelMap().put("newStudent", student);
		return mav;
	}


	@RequestMapping(value="/newAddmissionForm", method=RequestMethod.GET)
	public ModelAndView newAddmissionForm()
	{
		ModelAndView mav = new ModelAndView("admission/admissionForm");
		AdmissionFormView view = new AdmissionFormView();
		mav.getModelMap().put("admissionForm", view);
		
		Long admissionNo = studentService.getAdmissionNo();
		mav.addObject("admissionNo", admissionNo);

		List<Section> section = studentService.getClassAndSections();		
		mav.addObject("section", section);
		
		List<StudentCategory> category = studentService.getStudentCategory();		
		mav.addObject("category", category);
	
		return mav;
	}
	

	@RequestMapping(value="/editAddmissionForm", method=RequestMethod.GET)
	public ModelAndView editAddmissionForm(@RequestParam("id")Long admissionNo)
	{
		ModelAndView mav = new ModelAndView("admission/admissionForm");
		
		List<Section> section = studentService.getClassAndSections();		
		mav.addObject("section", section);
		List<StudentCategory> category = studentService.getStudentCategory();		
		mav.addObject("category", category);
		AdmissionFormView admissionForm = studentService.getAdmissionFormByAdmNo(admissionNo);		
		mav.addObject("admissionForm", admissionForm);
		mav.addObject("admissionNo", admissionForm.getAdmissionNo());		
		
		return mav;
	}

	
	@RequestMapping(value="/saveAddmissionForm", method=RequestMethod.POST)
	public String saveAddmissionForm(@ModelAttribute("admissionForm")AdmissionFormView form, BindingResult result, SessionStatus status)
	{
		Long id = studentService.saveAddmissionForm(form);
		status.setComplete();
		return "redirect:editStudentCategory.htm?id="+id;
	}

	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	@RequestMapping(value="/managestudentcategory", method=RequestMethod.GET)
	public ModelAndView manageClass()
	{
		ModelAndView mav = new ModelAndView("student/category/manageStudentCategory");
		List<StudentCategory> categoryList = studentService.getCategoryList();
		mav.addObject("categoryList", categoryList);
		return mav;
	}
	@RequestMapping(value="/addStudentCategory", method=RequestMethod.GET)
	public ModelAndView addStudentCategory()
	{
		ModelAndView mav = new ModelAndView("student/category/addStudentCategory");
		StudentCategory category = new StudentCategory();
		mav.getModelMap().put("category", category);
		return mav;
	}
	
	@RequestMapping(value="/saveStudentCategory", method=RequestMethod.POST)
	public String saveStudentCategory(@ModelAttribute("category")StudentCategory category, BindingResult result, SessionStatus status)
	{
		Long id = studentService.saveCategory(category);
		status.setComplete();
		return "redirect:managestudentcategory.htm" ;
	}
	
	@RequestMapping(value="/editStudentCategory", method=RequestMethod.GET)
	public ModelAndView editStudentCategory(@RequestParam("id")Long categoryId)
	{
		ModelAndView mav = new ModelAndView("student/category/editStudentCategory");
		StudentCategory category = studentService.getStudentCategoryById(categoryId);
		mav.addObject("category", category);
		return mav;
	}  
	
	@RequestMapping(value="/updateStudentCategory", method=RequestMethod.POST)
	public String updateStudentCategory(@ModelAttribute("updateStudentCategory")StudentCategory category, BindingResult result, SessionStatus status,
			@RequestParam("id")Long categoryId)
	{
		studentService.update(category,categoryId);	
		status.setComplete();
		return "redirect:managestudentcategory.htm" ;
	}

	@RequestMapping(value="/deleteStudentCategory", method=RequestMethod.GET)
	public String deleteStudentCategory(@RequestParam("id")Long categoryId , HttpServletRequest request)
	{
		String status = studentService.deleteStudentCategory(categoryId);
		return "redirect:managestudentcategory.htm" ;
	}

	@RequestMapping(value="/manageStudent", method=RequestMethod.GET)
	public ModelAndView getStudentInfo()
	{
		ModelAndView mav = new ModelAndView("student/manageStudent");		
		List<Section> sectionList = sectionService.getSectionList();;
		mav.addObject("sectionList", sectionList);
		return mav;
	}  

	@RequestMapping(value="/studentProfile", method=RequestMethod.GET)
	public ModelAndView getStudentProfile(@RequestParam("id")Long studentId)
	{
		ModelAndView mav = new ModelAndView("student/studentProfile");
		Student student = studentService.getStudentByStudentId(studentId);
		mav.addObject("student", student);
		return mav;
	}  
	@RequestMapping(value="/bulkStudentUpload", method=RequestMethod.GET)
	public ModelAndView bulkStudentUpload()
	{
		ModelAndView mav = new ModelAndView("student/bulkStudentUpload");		
		return mav;
	}
	
	@RequestMapping(value="/upload", method=RequestMethod.POST)
	  public ModelAndView upload(@RequestParam("file") MultipartFile uploadFile)throws Exception
	  {
	    ModelAndView mav = new ModelAndView("student/viewExcelUpload");
	    try {
	      if (uploadFile != null) {
	        List<AdmissionFormView> studentList = studentService.getAdmissionFormData(uploadFile.getInputStream());
	        if ((studentList != null) && (studentList.size() > 0)) {	        	
	        	studentService.saveBulkAddmissionForm(studentList);
	          mav.addObject("studentList", studentList);
	          mav.addObject("studentListCount", Integer.valueOf(studentList.size()));
	        }
	      }
	    } finally {
	      try {
	        if (uploadFile.getInputStream() != null)
	          uploadFile.getInputStream().close();
	      } catch (Exception exp) {
	    	  exp.printStackTrace();
	      }
	    }
	    return mav;
	  }

	  @RequestMapping(value = "/ajax/getStudentsListJson/{name}", method = RequestMethod.GET)	  
	  public @ResponseBody List<UserDetailsDTO> getStudentsListJson(@PathVariable("name") String name) {
		  try {
			  List<UserDetailsDTO> list = studentService.getStudentsByName(name);
			  return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		  return null;

	 }
		@RequestMapping(value="/ajax/getStudentListAjax", method=RequestMethod.GET)
		public @ResponseBody String getStudentListAjax(@RequestParam("id")Long sectionId , HttpServletRequest req)
		{
			List<Student> studentList = studentService.getStudentsBySectionId(sectionId);
			String ajax = studentService.processStudentList(studentList ,req);
			return ajax;
		}
	  
	  
}
